package sz.szsmk.citizencard.mvp.presenter;

import com.google.gson.Gson;

import javax.inject.Inject;

import sz.szsmk.citizencard.log.L;
import sz.szsmk.citizencard.model.network.GsdxNetApi;
import sz.szsmk.citizencard.model.network.bean.request.CheckCodeRequest;
import sz.szsmk.citizencard.model.network.bean.request.GetCodeRequest;
import sz.szsmk.citizencard.model.network.bean.response.BaseBean;
import sz.szsmk.citizencard.model.network.bean.response.IbaseBean;
import sz.szsmk.citizencard.mvp.contract.IModifyPhoneContact;
import sz.szsmk.citizencard.retrofitrxjava.ErrorAction;
import sz.szsmk.citizencard.retrofitrxjava.NetworkDateSources;
import sz.szsmk.citizencard.retrofitrxjava.SubscribeResult;
import sz.szsmk.citizencard.util.Constant;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/7/10 14:35
 * description:
 */
public class ModifyPhonePresenter extends IBasePresenter
        implements IModifyPhoneContact.IModifyPhonePresenter<IModifyPhoneContact.IModifyPhoneView> {

    private NetworkDateSources mNetworkDateSource;
    GsdxNetApi gsdxNetApi;

    @Inject
    public ModifyPhonePresenter(GsdxNetApi gsdxNetApi) {
        this.gsdxNetApi = gsdxNetApi;

        this.mNetworkDateSource = NetworkDateSources.getInstance();
    }

    @Override
    public void getCode(String userPhone, String androidId) {


        GetCodeRequest getCodeRequest = new GetCodeRequest();
        getCodeRequest.setDevice_id(androidId);
        getCodeRequest.setChannel_id("000001");
        getCodeRequest.setType_id("4");
        getCodeRequest.setMobile(userPhone);
        mNetworkDateSource.getSms(getCodeRequest, new SubscribeResult<BaseBean>() {
            @Override
            protected void onOk(BaseBean response) {
                ((IModifyPhoneContact.IModifyPhoneView) mView).getCodeSuccess();
            }

            @Override
            protected void onFailure(BaseBean response) {
                super.onFailure(response);
                ((IModifyPhoneContact.IModifyPhoneView) mView).getCodeFalie(response);
            }

            @Override
            protected void onNext(BaseBean response) {
                super.onNext(response);
                ((IModifyPhoneContact.IModifyPhoneView) mView).getCodeFalie(response);
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }

    @Override
    public void checkCode(String code, String userPhone, String androidId) {
        CheckCodeRequest codeRequest = new CheckCodeRequest();
        codeRequest.setDevice_id(androidId);
        codeRequest.setMobile(userPhone);
        codeRequest.setSms_code(code);
        codeRequest.setSms_type("4");
        String json = new Gson().toJson(codeRequest);
        L.e(Constant.TAG, "checkSms===" + json);
        mNetworkDateSource.checkSms(codeRequest, new SubscribeResult<IbaseBean>() {
            @Override
            protected void onOk(IbaseBean response) {
                ((IModifyPhoneContact.IModifyPhoneView) mView).checkCodeSucc(response.getMessage());
            }

            @Override
            protected void onFailure(IbaseBean response) {
                super.onFailure(response);
                ((IModifyPhoneContact.IModifyPhoneView) mView).checkCodeFail(response.getMessage());
            }

            @Override
            protected void onNext(IbaseBean response) {
                super.onNext(response);
                ((IModifyPhoneContact.IModifyPhoneView) mView).checkCodeFail(response.getMessage());
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
                ((IModifyPhoneContact.IModifyPhoneView) mView).checkCodeFail(throwable.getMessage());
            }
        });
    }

    @Override
    public void updateUserPhone(String mobile) {
        GetCodeRequest getCodeRequest = new GetCodeRequest();
        getCodeRequest.setMobile(mobile);
        mNetworkDateSource.updateUserPhone(getCodeRequest, new SubscribeResult<BaseBean>() {
            @Override
            protected void onOk(BaseBean response) {
                String json = new Gson().toJson(response);
                L.e(Constant.MTAG, "----" + json);
                ((IModifyPhoneContact.IModifyPhoneView) mView).updateUserPhoneSucc(response.getMessage());
            }

            @Override
            protected void onFailure(BaseBean response) {
                super.onFailure(response);
                ((IModifyPhoneContact.IModifyPhoneView) mView).updateUserPhoneFail(response.getMessage());
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {

            }
        });
    }

}
